/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.twistinjava.action;

import com.twistinjava.logon.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 *
 * @author shikhar
 */
public abstract class BaseAction extends org.apache.struts.action.Action
{

    private static Log log = LogFactory.getLog(BaseAction.class);
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {

        log.debug("Beginning of Request chain");

        // Note: This method will not be called for LogonAction. There
        // we will not be checking if the user is already in Session.

        ActionForward af = null;
        HttpSession session = request.getSession(false);

        // Is there a logged in user in session ??

        // Do we have a session?
        if (session == null)
        {
            //if session is null then there can not be a logged in user.
            log.debug("We have a null http session");

            ActionMessages ae = new ActionMessages();
            ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("session.expired"));
            saveMessages(request, ae);

            //request.setAttribute(HibernateSessionConversationFilter.END_OF_CONVERSATION_FLAG, "true");
            af = mapping.findForward("failure");
        }
        else //session is not null
        {
            // Do we have a user?
            User authUser = (User) session.getAttribute("user");

            if (authUser == null)
            {
                //log and send to logon page

                log.info("Trying to access system without being logged in");

                ActionMessages ae = new ActionMessages();
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("session.expired"));
                saveMessages(request, ae);
                af = mapping.findForward("failure");
            }
            else
            {
                 af = twistinjavaExecute(mapping, form, request, response);
                //good - continue working with called action

            }
        }

        return af;

    }

    public abstract ActionForward twistinjavaExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception;
}
